﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuatToanDAO;
using System.Data.SqlClient;
using System.Data;

public partial class Admin_Categories : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Session["UserName"] == null))
        {
            Response.Redirect("Default.aspx?path=" + Request.Url.ToString());
        }
        
        if (!IsPostBack)
        {
            Panel_Add.Visible = false;
            Panel_Edit.Visible = false;
        }
        
        
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {

        string categoryid = GridView1.SelectedRow.Cells[1].Text;

        CategoryDAO category = new CategoryDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        parameters.Add(new SqlParameter("CategoryID", categoryid));
        parameters.Add(new SqlParameter("Name", txtName_Edit.Text));

        category.ExecuteUpdate(parameters);

        GridView1.DataBind();

        lbl_Message.Text = "Edit successfully!";
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string categoryid = GridView1.SelectedRow.Cells[1].Text;
        CategoryDAO category = new CategoryDAO();
        DataTable dt = category.ExecuteSelectSingle(categoryid).Tables[0];

        txtCategoryID_Edit.Text = dt.Rows[0]["CategoryID"].ToString();
        txtName_Edit.Text = dt.Rows[0]["Name"].ToString();

        Panel_Add.Visible = false;
        Panel_Edit.Visible = true;
    }
    protected void btn_AddItems_Click(object sender, EventArgs e)
    {
        Panel_Add.Visible = true;
        Panel_Edit.Visible = false;
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        CategoryDAO category = new CategoryDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        
        parameters.Add(new SqlParameter("CategoryID", txtCategoryID_Add.Text));
        parameters.Add(new SqlParameter("Name", txtName_Add.Text));

        category.ExecuteInsert(parameters);

        GridView1.DataBind();
        reset();
        lbl_Message.Text = "Add successfully!";

    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        string categoryid = GridView1.SelectedRow.Cells[1].Text;

        CategoryDAO category = new CategoryDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        parameters.Add(new SqlParameter("CategoryID", categoryid));
        category.ExecuteDelete(parameters);

        lbl_Message.Text = "Delete successfully!";

        GridView1.DataBind();
        Panel_Edit.Visible = false;
    }

    protected void reset()
    {
        txtCategoryID_Add.Text = "";
        txtName_Add.Text = "";
    }
}